package demo.practice.leetcode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Ms0403 {


    public ListNode[] listOfDepth(TreeNode tree) {
        if(tree==null)
            return new ListNode[0];
        List<ListNode> list = new ArrayList<>();

        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(tree);
        while (!queue.isEmpty()) {
            int size = queue.size();
            ListNode root = null;
            ListNode current = null;
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                if (poll != null) {
                    if (root == null) {
                        root = new ListNode(poll.val);
                        current = root;
                    } else {
                        current.next = new ListNode(poll.val);
                        current = current.next;
                    }
                    if (poll.left != null)
                        queue.add(poll.left);
                    if (poll.right != null)
                        queue.add(poll.right);
                }
            }
            list.add(root);
        }
        ListNode[] nds = new ListNode[list.size()];
        return list.toArray(nds);
    }
}
